﻿using System;

using Microsoft.Xna.Framework;

namespace BFTest
{
    static class BattlefieldVectorExtensions
    {
        public static float RotationToTarget(this Vector2 vec, Vector2 target)
        {
            Vector2 direction = target - vec;

            return (float)Math.Atan2(direction.Y,direction.X);
        }

        public static float Rotation(this Vector2 vec)
        {
            return (float)Math.Atan2(vec.Y, vec.X);
        }

        // todo: benchmark to see if an out vector is faster than a copy
        public static Vector2 RotateVector(this Vector2 vec, float angle)
        {
            vec.X = vec.X * (float)Math.Cos(angle) - vec.Y * (float)Math.Sin(angle);
            vec.Y = vec.X * (float)Math.Sin(angle) + vec.Y * (float)Math.Cos(angle);

            return vec;
        }

        public static Vector2 Perpindicular(this Vector2 vec)
        {
            vec.X = -vec.Y;
            vec.Y = vec.X;

            return vec;
        }

        public static Vector2 PointToLocalSpace(this Vector2 point,Vector2 localOrigin, Vector2 localHeading)
        {

            float angle = (float)Math.Atan2(localHeading.Y, localHeading.X);
            if (angle < 0)
                angle += MathHelper.TwoPi;
            point = Vector2.Transform(point - localOrigin, Matrix.CreateRotationZ(-angle));

            return point;
        }
    }
}
